 <template>
  <div>
    <Activate :isActivate="activated" @to_activate="toActivate"></Activate>

    <div class="card-style total-item">
      <div class="flex-around">
        <div>
          <img src="../../../images/total.png" alt>
        </div>
        <div class="flex-around">
          <div>
            <span class="label-text">余额</span>
            <span class="price">￥{{balance || 0}}</span>
          </div>
          <div>
            <span class="label-text">冻结金额</span>
            <span class="price">￥{{lockAmount || 0}}</span>
          </div>
          <div>
            <el-button
              @click="toRequest"
              style="margin-left:10px;color='#ddd'"
              size="small"
              type="danger"
              plain
            >申请提现</el-button>
          </div>
        </div>
      </div>
    </div>

    <el-dialog title="提现" :visible.sync="showDialog" width="50%">
      <el-form label-width="100px" :model="formData" ref="formData">
        <el-form-item label="提现方式">
          <el-select v-model="accountIndex" placeholder="请选择">
            <el-option v-for="(item, index) in type" :key="index" :label="item.name" :value="index"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="提现金额"
          prop="count"
          :rules="[{require: true, message: '请输入金额', trigger: 'blur'},{validator: validatePass, trigger: ['blur', 'change']}]"
        >
          <el-input style="width:300px" v-model="formData.count" placeholder="请输入提现金额"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="showDialog = false">取 消</el-button>
        <el-button class="add-style" type="primary" @click="advanceReqest">提现</el-button>
      </span>
    </el-dialog>

    <el-tabs v-model="activeName" @tab-click="changeTab">
      <el-tab-pane label="待接受" name="PENDING">
        <List :tableData="tableData" :isRecall="true" @recall="recall"></List>
      </el-tab-pane>
      <el-tab-pane label="提现中" name="PROGRESSING">
        <List :tableData="tableData"></List>
      </el-tab-pane>
      <el-tab-pane label="已提现" name="DONE">
        <List :tableData="tableData"></List>
      </el-tab-pane>
      <el-tab-pane label="已驳回" name="REJECTED">
        <List :tableData="tableData"></List>
      </el-tab-pane>
      <el-tab-pane label="已撤回" name="CANCEL">
        <List :tableData="tableData"></List>
      </el-tab-pane>
    </el-tabs>

    <Pagination :total="total" @handleChange="handleChange"/>
  </div>
</template>

<script>
import {
  getOwnerRequest,
  addRequest,
  getAccountInfo,
  cancelOwnerRequest
} from "api/account";
import List from "./list.vue";
import Activate from "src/components/activate/activate.vue";
import { mapState, mapMutations } from "vuex";
import { getOrganization } from "api/profile";
import { formatDateTime } from "src/util/time.js";
import Pagination from "src/components/pagination";

export default {
  data() {
    let validatePass = (rule, value, callback) => {
      var reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
      if (!reg.test(value)) {
        callback(new Error("请输入正确的金额"));
      } else if (value > this.balance) {
        callback(new Error("提现金额不能超出账户余额"));
      } else {
        callback();
      }
    };
    return {
      activeName: "PENDING",
      tableData: [],
      count: "", // 提现金额
      activated: true, // 企业是否激活
      accountIndex: 0, // 当前选择的支付账号序号，默认【企业支付宝】
      showDialog: false, // 显示提现的dialog
      balance: 0, // 总收入
      lockAmount: 0, // 冻结金额
      formData: {
        count: ""
      },
      type: [
        {
          name: "企业支付宝",
          account: "123",
          type: "ALI"
        },
        {
          name: "银行",
          account: "456",
          type: "BANK"
        }
      ],
      validatePass: validatePass,
      total: -1,
      currentName: "PENDING" // 当前选择的tab
    };
  },
  computed: {
    ...mapState(["accountInfo", "userInfo"])
  },
  components: { List, Activate, Pagination },
  mounted() {
    this.GET_ACCOUNT(); // 从缓存获取账户数据
    this.balance = this.accountInfo.balance;
    this.GET_USERINFO();
    this.activated = this.userInfo.activated; // 是否激活标识

    this.getData("PENDING", 1);
    this.init();
  },
  methods: {
    ...mapMutations(["GET_ACCOUNT", "GET_USERINFO"]),
    advanceReqest() {
      this.$refs.formData.validate(val => {
        if (val) {
          let data = {
            from: 'ORG',
            amount: this.formData.count,
            outerAccount: this.type[this.accountIndex].account,
            outerPayMethod: this.type[this.accountIndex].type
          };
          addRequest(data).then(res => {
            this.getBalance();
            this.getData("PENDING");
            this.$message.success("成功发起提现，请等待平台审核");
            this.showDialog = false;
            this.formData.count = "";
          });
        } else {
          return false;
        }
      });
    },
    toRequest() {
      if (!this.activated) {
        this.$message.error("企业未激活，请激活后再提现");
        return;
      }
      this.showDialog = true;
    },
    changeTab(tab, event) {
      this.getData(tab.name);
      this.currentName = tab.name;
    },
    getData(type, pageNumber) {
      getOwnerRequest(type, pageNumber).then(res => {
        this.total = res.totalElements;

        this.tableData = res.content.map(item => {
          item.updateTime = formatDateTime(item.updateTime);
          return item;
        });
      });
    },
    handleChange: function(pageNumber) {
      this.getData(this.currentName, pageNumber);
    },
    toActivate() {
      this.$router.push("/profile");
    },
    init() {
      this.getBalance();
      getOrganization().then(res => {
        this.type[0].account = res.accAlipay;
        this.type[1].account = res.accBankSn;
      });
    },
    getBalance() {
      getAccountInfo().then(res => {
        this.balance = res.balance;
        this.lockAmount = res.lockAmount;
      });
    },
    recall(id) {
      cancelOwnerRequest(id).then(res => {
        this.$message.success("撤回成功");
        this.getData("PENDING");
        this.getBalance();
      });
    }
  }
};
</script>

<style lang="scss" scoped>
@import "src/style/mixin";
.tabList {
  @include fj(flex-start);

  .tabItem {
    padding-left: 20px;
  }
}
// -------------卡片--------------------------
.count-cards {
  @include fj(flex-start);
  align-items: center;
}
.wrap-item {
  flex-wrap: wrap;
}
.card-style {
  width: 600px;
  margin: 10px;
  padding: 10px;
  border-radius: 10px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.box-card {
  height: 100px;
  color: #fff;
}

.flex-around {
  @include fj(space-between);
  align-items: center;
  padding: 0 15px;

  .text-right {
    text-align: right;
  }
  .label-text {
    @include sc(12px, #f1f1f1);
    margin-left: 10px;
  }
  .price {
    @include sc(30px, #fff);
  }
}

.total-item {
  background: #f56c6c;
  // width: 260px;
}

// -------------卡片--------------------------
</style>
